
var canvas = document.getElementById("gameCanvas") as HTMLCanvasElement;
var context = canvas.getContext("2d");
var y = 50;
var a = new Image();
a.src="1.jpg";
var b= new Image();
b.src="tile1.jpg";
var d=new Image();
d.src="npc2.png";
const i=new Image();
i.src="begin2.png";
const j=new Image();
j.src="begin1.png";

const m=new  Image();
const n=new Image();
m.src="boy1.png";
n.src="girl1.png";



var x=0;
const v=new Image();
v.src="entergame.png";

const bag=new Image();
bag.src="bag.png";


class LoginState extends fsm.State {

    private textField: renderer.TextField;
    private bitmp:renderer.Bitmap;
    private bitmp1:renderer.Bitmap;
    private bitmp2:renderer.Bitmap;
    private audio:HTMLAudioElement;
    
    
    private onClick = (eventData: any) => {
      gameFsm.replaceState(new UserState());

    }
  

  

    onEnter() {
         this.audio=document.createElement("audio");
        this.audio.src="登陆.mp3";
        this.audio.play();
       // this.textField = new renderer.TextField(100, 100);
       // this.textField.text = '点击进入';
        this.bitmp=new renderer.Bitmap(100,100,i);
        this.bitmp1=new renderer.Bitmap(100,100,v);
        this.bitmp1.x=400;
        this.bitmp1.y=500;
       /* this.bitmp=new renderer.Bitmap(200,100,a);
        this.bitmp=new renderer.Bitmap(300,100,b);
        

        */stage.addChild(this.bitmp);
       /* stage.addChild(this.bitmp1);
        stage.addChild(this.bitmp2);*/

        //stage.addChild(this.textField);
        stage.addChild(this.bitmp1);
        
        this.bitmp1.addEventListener("click", this.onClick);
        
    }



    onExit() {

        stage.removeEventListener("click", this.onClick);
       // stage.removeChild(this.textField);
        stage.removeChild(this.bitmp);
        stage.removeChild(this.bitmp1);
        this.audio.pause();
        this.audio.currentTime=0;
      /*  stage.removeChild(this.bitmp1);

        stage.removeChild(this.bitmp2);*/

    }
}

/**
 * 用户面板 UI 界面
 * MVC:View
 */
class UserPanelUI extends renderer.DisplayObjectContainer {

    constructor() {
        super();
        const userNameLabel = new renderer.TextField(650, 0);
        this.addChild(userNameLabel);
        const userLevelLabel = new renderer.TextField(680, 0);
        this.addChild(userLevelLabel);

        const userAttackLabel = new renderer.TextField(710,15);
        this.addChild(userAttackLabel);

        const usersliverCoinLabel=new renderer.TextField(710,0);
        this.addChild(usersliverCoinLabel);

        userLevelLabel.text = "lv:" + user.level;
        userNameLabel.text = user.name;
        userAttackLabel.text = "attack:" + user.attack;
        usersliverCoinLabel.text="sliverCoin: "+user.sliverCoin;

        user.addEventListener("userchange", () => {
            userLevelLabel.text = "lv:" + user.level;
            userNameLabel.text = user.name;
            userAttackLabel.text = "attack:" + user.attack;
            usersliverCoinLabel.text="sliverCoin: "+ user.sliverCoin;
            console.log(user.level);
        })
    }
}

const RIVER = 0;
const GRESS = 1;
const TILE_SIZE = 64;





let dialogContainer: renderer.DisplayObjectContainer
let ItemDialogContainer:renderer.DisplayObjectContainer;
let monsterDialogContainer:renderer.DisplayObjectContainer;

class UserState extends fsm.State{
    private textField: renderer.TextField;
    private bitmp3:renderer.Bitmap;
    private bitmp1:renderer.Bitmap;
    private bitmp2:renderer.Bitmap;
    private fsm = new fsm.StateMachine();
    private audioElement:HTMLAudioElement;
    private b1:renderer.DisplayObjectContainer;
    private b2:renderer.DisplayObjectContainer;
    private onClicka = (eventData: any) => {
        user=new User();
        user.createUsera();
        gameFsm.replaceState(new PlayingState());


        
      
      this.onExit();
        
    }
    private onClickb = (eventData: any) => {
        user=new User();
        user.createUser();
        gameFsm.replaceState(new PlayingState());

        
      
      this.onExit();
        
    }
   /* private createUser() {

        user = new User();
        user.level = 1;
        user.name = 'boge';
        user.view=new renderer.Bitmap(0,0,d);


       
        missionManager.init();



        console.log(user.toString())
    }*/
    onEnter() {
        this.textField=new renderer.TextField(400,100);
        this.textField.text="角色一";
      //  this.bitmp1=new renderer.Bitmap(200,100,a);
        // this.bitmp2=new renderer.Bitmap(400,100,d);
        // this.bitmp1=new renderer.Bitmap(200,100,b);
        this.audioElement=document.createElement("audio");
        this.audioElement.src='登陆.mp3';
        this.audioElement.play();
        this.bitmp2=new renderer.Bitmap(50,50,n);
        this.bitmp1=new renderer.Bitmap(50,50,m);
        this.bitmp3=new renderer.Bitmap(100,100,i);
        this.bitmp1.scaleX=this.bitmp1.scaleY=0.3;
        this.bitmp2.scaleX=this.bitmp2.scaleY=0.3;


        this.bitmp2.x=450;
        this.bitmp2.y=300;
        this.bitmp1.x=150;
        this.bitmp1.y=300;

       this. b1=new renderer.DisplayObjectContainer();
       this. b2=new renderer.DisplayObjectContainer();
       this.b1.addChild(this.bitmp1);
       this.b1.addChild(this.bitmp2);
       this.b2.addChild(this.bitmp3);


       stage.addChild(this.b2);

        stage.addChild(this.b1);
        this.bitmp2.addEventListener("click", this.onClicka);
        this.bitmp1.addEventListener("click", this.onClickb);




}
   onExit(){
      stage.removeChild(this.b1);
      stage.removeChild(this.b2);

      console.log(stage.children)
      this.bitmp2.removeEventListener("click",this.onClicka);
      this.bitmp1.removeEventListener("click",this.onClickb);
      this.audioElement.pause();
      this.audioElement.currentTime=0;
   }
}
let openNode:string='';
let OpenNode:string='';

class PlayingState extends fsm.State {
    private fsm = new fsm.StateMachine();

    onEnter() {


        
        const missionPanel = new MissionPanel();
        const mapContainer = new renderer.DisplayObjectContainer();
        mapManager.initContainer(mapContainer);
        const uicontainer = new renderer.DisplayObjectContainer();
        const userPanel = new UserPanelUI();
        dialogContainer = new renderer.DisplayObjectContainer();
        ItemDialogContainer=new renderer.DisplayObjectContainer();
        monsterDialogContainer=new renderer.DisplayObjectContainer();

        const bag1=new renderer.Bitmap(0,0,bag);
        bag1.x=650;
        bag1.y=180;
        bag1.scaleX=bag1.scaleY=0.2;
       // bag1.alpha=0.4;
        bag1.addEventListener("click",()=>{
            if(OpenNode=="open"){
                return ;
            }else{
                const bagDialog=new itemDialog();
                
               ItemDialogContainer.addChild(bagDialog);
               bagDialog.addEventListener("closeBag",()=>{
                ItemDialogContainer.removeChild(bagDialog);
               OpenNode=' ';
              })
              OpenNode="open";
            }
            
            
        })


        stage.addChild(mapContainer);
        stage.addChild(uicontainer);

        uicontainer.addChild(userPanel);
        uicontainer.addChild(missionPanel);
        uicontainer.addChild(dialogContainer);
        uicontainer.addChild(ItemDialogContainer);
        uicontainer.addChild(monsterDialogContainer);

        uicontainer.addChild(bag1);
        const mapName: string[] = ["map2","map3"];


       
       
        missionPanel.x = 250;
        missionPanel.y = 100;
        mapContainer.x = mapContainer.y = 0;

        mapManager.changeMap("map",user.view.x/TILE_SIZE,user.view.y/TILE_SIZE);
        const gotoMap = (eventData: any) => {
            const mapname = mapName.shift();
            if (mapname) {
                const goToAnotherMap = new GotoAnotherMap(mapname);
                pool.add(goToAnotherMap);
            } else {
                return ;
            } 
           
           
        }

        user.addEventListener("changeMap", gotoMap); 
        
       
        
    }



    onUpdate() {

        user.update();
       
    }
}



let pool = new command.CommandPool();
let user: User;

gameFsm.replaceState(new LoginState());


function loadConfig(url: string, callback: Function) {
    const loader = new XMLHttpRequest();
    loader.open("get", url);
    loader.send();
    loader.onload = () => {
        const obj = JSON.parse(loader.response);
        callback(obj);
    }
}

loadConfig("mission.json", (obj: any) => {
    missionManager.parseFromConfig(obj);
    loadConfig("npc.json", (obj: any) => {
        npcManager.parseFromConfig(obj);
        loadConfig("item.json", (obj: any) => {
            itemManager.parseFromConfig(obj);
            loadConfig("monster.json",(obj: any) =>{
                monsterManager.parseFromConfig(obj);
                loadConfig("map.json", (obj: any) => {
                    mapManager.parseFromConfig("map", obj);
                    loadConfig("map2.json", (obj: any)=> {
                        mapManager.parseFromConfig("map2", obj);
                        loadConfig("map3.json", (obj: any) => {
                                mapManager.parseFromConfig("map3", obj);
                        })
                    
                    })
                })
            })
        })
       
    })
})

